<template>
    <div class="index clearafter">
        <module-head></module-head>
        <div class="content flex">
            <div class="w220">
                <menu-list></menu-list>
            </div>
            <div class="w980 flex_full">
                <div class="flex flex_between">
                  <p class="flex flex_vcenter pdb20">
                    <i class="titleTag"></i>
                    <span class="fRedBig">产品版本管理</span>
                  </p>
                    <button class="longBtn" @click="addproduct()">新增产品版本</button>
                    <div id="searchbyname">
                        <form class="form-horizontal" role="form">
                            <div class="form-group">
                                <label class="col-md-4 control-label">产品名称:</label>
                                <div class="col-md-8">
                                    <select class="form-control" v-model="proitem">
                                        <option v-for="(index,item) in prolists" value="{{item.code+'/'+item.name}}">{{item.name}}</option>
                                    </select>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
                <div class="servicepeoplelist bg">
                    <table class="table table-hover table-striped">
                        <thead>
                            <tr>
                                <th>产品编码</th>
                                <th>产品名称</th>
                                <th>平台类型</th>
                                <th>版本</th>
                                <th>强制更新</th>
                                <th>版本说明</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr v-for="item in productlists" track-by="$index">
                                <td>{{item.productCode}}</td>
                                <td>{{item.productName}}</td>
                                <td>{{item.platform==1?"ios":"android"}}</td>
                                <td>{{item.version}}</td>
                                <td>{{item.updateType==1?"是":"否"}}</td>
                                <td>{{item.content}}</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <!-- 新增 -->
        <div class="modal fade" id="addproduct" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h4 class="modal-title" id="myModalLabel">新增产品版本</h4>
                    </div>
                    <form class="form-horizontal cb" role="form" v-on:submit.prevent="saveproduct">
                        <div class="modal-body">
                            <div class="form-group">
                                <label class="col-md-3 control-label">产品名称:<span class="start">*</span></label>
                                <div class="col-md-8">
                                    <!-- <select class="form-control" v-model="savedata.productCode" required>
                                        <option v-for="(index,item) in prolists" value="{{item.code}}">{{item.name}}</option>
                                    </select> -->
                                    <input type="text" class="form-control" required v-model="currentname" disabled />
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-md-3 control-label">产品编码:<span class="start">*</span></label>
                                <div class="col-md-8">
                                    <input type="text" class="form-control" required v-model="savedata.productCode" disabled />
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-md-3 control-label">平台类型:<span class="start">*</span></label>
                                <div class="col-md-8">
                                    <select class="form-control" required v-model="savedata.platform">
                                        <option value="1">ios</option>
                                        <option value="2">android</option>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-md-3 control-label">版本:<span class="start">*</span></label>
                                <div class="col-md-8">
                                    <input type="text" class="form-control" required v-model="savedata.version" />
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-md-3 control-label">下载地址:<span class="start">*</span></label>
                                <div class="col-md-8">
                                    <input type="text" class="form-control" required v-model="savedata.url" maxlength="100" />
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-md-3 control-label">强制更新:</label>
                                <div class="col-md-8">
                                    <div class="radio">
                                        <label class="checkbox-inline">
                                            <input type="radio" name="sex" value="1" v-model="savedata.updateType">是
                                        </label>
                                        <label class="checkbox-inline">
                                            <input type="radio" name="sex" value="0" v-model="savedata.updateType">否
                                        </label>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-md-3 control-label">版本说明:</label>
                                <div class="col-md-8">
                                    <textarea class="form-control" rows="3" v-model="savedata.content" maxlength="100"></textarea>
                                </div>
                            </div>
                        </div>
                        <div class="modal-footer savebtn">
                            <button type="submit" class="default ok center">确认</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
import 'assets/lib/bootstrap-3.3.2/css/bootstrap.min.css'
import 'assets/lib/bootstrap-3.3.2/js/bootstrap.min.js'
import 'assets/reset.css'
import Public from 'assets/public.js'
import moduleHead from 'components/module-head'
import menuList from 'components/menu-list'
export default {
    data() {
            return {
                productlists: [], //版本列表
                prolists: [], //产品下拉列表
                savedata: {
                    "content": "",
                    "memo": "",
                    "platform": "",
                    "productCode": "",
                    "updateType": 1,
                    "url": "",
                    "version": "",

                },
                proitem: "",
                currentname: "",
                currentcode: ""
            }
        },
        computed: {},
        components: {
            moduleHead, menuList
        },
        ready() {
            var that = this;
            //获取产品下拉框列表
            $.when(Public.commonajax("base.tenantProductsService", "queryProductTenantByPcode", "[null]"))
                .done(function(res) {
                    if (res.code == 200) {
                        if (that.prolists.length == 0) {
                            that.prolists = res.body;
                            that.proitem = res.body[0].code + '/' + res.body[0].name;

                        }
                        that.prolists = res.body;
                    }

                });
        },
        methods: {
            // 获取列表数据
            getlist(productcode) {
                    var that = this;
                    $.when(Public.commonajax("base.versionService", "findVersions", "['" + productcode + "']"))
                        .done(function(res) {
                            if (res.code == 200) {
                                that.productlists = res.body;
                            }
                        })
                },

                // 新增弹框
                addproduct() {
                    if (this.productlists.length > 0) {
                        this.savedata = {
                            "content": "",
                            "memo": "",
                            "platform": this.productlists[0].platform,
                            "productCode": this.currentcode,
                            "updateType": 1,
                            "url": this.productlists[0].url,
                            "version": "",

                        };
                    } else {
                        this.savedata = {
                            "content": "",
                            "memo": "",
                            "platform": "",
                            "productCode": this.currentcode,
                            "updateType": 1,
                            "url": "",
                            "version": "",

                        };
                    }

                    $("#addproduct").modal("show");

                },
                // 保存修改或新增
                saveproduct() {
                    var that = this;
                    var regx=/^\d{1,2}(\.\d{1,2}){1,2}$/;
                     console.log(regx.test(this.savedata.version));
                    if(!regx.test(this.savedata.version)){
                         Public.ajaxPrompt("请输入正确格式的版本号");
                        return
                    }
                    if (this.productlists.length > 0) {
                        console.log(this.savedata.version>this.productlists[0].version );
                        if (this.savedata.version <= this.productlists[0].version) {
                            Public.ajaxPrompt("输入的版本号不能小于已有的版本号");
                        } else {
                            $.when(Public.commonajax("base.versionService", "saveVersion", "[" + JSON.stringify(this.savedata) + "]"))
                                .done(function(res) {
                                    if (res.code == 200) {
                                        $("#addproduct").modal("hide");
                                        Public.ajaxPrompt("保存成功");
                                         that.getlist(that.currentcode)
                                    }
                                })
                        }
                    } else {
                        $.when(Public.commonajax("base.versionService", "saveVersion", "[" + JSON.stringify(this.savedata) + "]"))
                            .done(function(res) {
                                if (res.code == 200) {
                                    $("#addproduct").modal("hide");
                                    Public.ajaxPrompt("保存成功");
                                     that.getlist(that.currentcode)
                                }
                            })
                    }


                },



        },
        events: {

        },
        watch: {
            'proitem': function(val, oldval) {
                var strarr = val.split("/");
                this.currentcode = strarr[0];
                this.currentname = strarr[1];
                this.getlist(this.currentcode)
            }
        },
}
</script>
<style scoped>
.servicepeoplelist {
    padding: 20px
}

.servicepeoplelist table th {
    text-align: center;
    line-height: 36px;
    height: 36px;
}

.servicepeoplelist table td {
    line-height: 30px;
    text-align: center;
}

.servicepeoplelist table tr td:nth-child(1) {
    width: 125px
}

.servicepeoplelist table tr td:nth-child(2) {
    width: 125px
}

.servicepeoplelist table tr td:nth-child(3) {
    width: 80px
}

.servicepeoplelist table tr td:nth-child(4) {
    width: 60px
}

.servicepeoplelist table tr td:nth-child(5) {
    width: 80px
}

#searchbyname {
    width: 400px;
    margin-right: 20px;
}

.centerbutton {
    width: 280px;
    margin: 0 auto;
}

.centerbutton button {
    margin-left: 5px;
    display: inline-block;
}

span.red {
    font-weight: bold;
    color: #f86448
}

.form-horizontal .checkbox-inline {
    padding-top: 4px
}
</style>
